package com.wbp.cms.client.export ;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;

public class BaseExportToExcel {

	/**
	 * 表头中文属性值，只支持单行表头
	 */
	private String[] headers = null;

	/**
	 * 表头的列属性
	 */
	private String[] headerAttributes = null;

	/**
	 * 表中数据值集合
	 */
	private List<Map<String, Object>> dataList = null;

	private HSSFWorkbook workbook = null;
	private String firstSheetName = "export";
	private HSSFSheet sheet = null;

	public BaseExportToExcel(String[] headers) {
		this(headers, null, "export");
	}

	public BaseExportToExcel(String[] headers, String firstSheetName) {
		this(headers, null, firstSheetName);
	}

	public BaseExportToExcel(String[] headers, String[] headerAttributes) {
		this(headers, headerAttributes, "export");
	}

	public BaseExportToExcel(String[] headers, String[] headerAttributes,
			String firstSheetName) {
		this(headers,headerAttributes,null,firstSheetName) ;
	}
	
	public BaseExportToExcel(String[] headers, String[] headerAttributes,List<Map<String, Object>> dataList) {
		this(headers,headerAttributes,dataList,"export") ;
	}
	
	public BaseExportToExcel(String[] headers, String[] headerAttributes,List<Map<String, Object>> dataList ,
			String firstSheetName) {
		this.headers = headers;
		this.headerAttributes = headerAttributes;
		this.firstSheetName = firstSheetName;
        this.dataList   =  dataList ;
		this.init();
	}

	private void init() {
		workbook = new HSSFWorkbook();
		sheet = workbook.createSheet(firstSheetName);
	}

	private void createHeader() {
		if (headers == null || headers.length == 0)
			throw new IllegalArgumentException("请先设置 headers表头属性!");

		if (headerAttributes == null || headerAttributes.length == 0)
			throw new IllegalArgumentException("请先设置 headerAttributes表头列属性!");
		// 构建表头
		HSSFRow headRow = sheet.createRow(0);
		HSSFCellStyle headStyle = getHeadStyle();
		for (int i = 0; i < headers.length; i++) {
			Cell cell = headRow.createCell(i);
			cell.setCellStyle(headStyle);
			cell.setCellValue(headers[i]);
		}
	}

	private void createData() {
		if (dataList == null || dataList.size() == 0)
			throw new IllegalArgumentException("请先设置 dataList属性!");
		HSSFCellStyle bodyStyle = getBodyStyle();
		for (int i = 0; i < dataList.size(); i++) {
			int cellIndex = 0;
			Map<String, Object> currMap = dataList.get(i);
			HSSFRow r = sheet.createRow(i + 1);  // 创建 一行 
			 
			 for(int j=0;j<headerAttributes.length;j++)   //写入列数据
			 {
				 Cell cell =  r.createCell(cellIndex++) ;
				 String  value = (currMap.get(headerAttributes[j]) == null ? "" : currMap.get(headerAttributes[j])+"") ;
				 cell.setCellStyle(bodyStyle);
				 cell.setCellValue(value) ;
			 }
			 
		}
	}
 
	/**
	 * 导出数据
	 * @param outputStream
	 */
	public void exportToExcel(OutputStream outputStream) {
		this.createHeader();
		this.createData();

		try {
			workbook.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 

	/**
	 * 设置表头的单元格样式
	 * 
	 * @return
	 */
	public HSSFCellStyle getHeadStyle() {
		// 创建单元格样式
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		// 设置单元格的背景颜色为红色
		cellStyle.setFillForegroundColor(HSSFColor.RED.index);
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		// 设置单元格居中对齐
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 设置单元格垂直居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 创建单元格内容显示不下时自动换行
		cellStyle.setWrapText(true);
		// 设置单元格字体样式
		HSSFFont font = workbook.createFont();
		// 设置字体加粗
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		font.setFontName("宋体");
		font.setFontHeight((short) 200);
		cellStyle.setFont(font);
		// 设置单元格边框为细线条
		/*
		 * cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		 * cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		 * cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		 * cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		 */
		return cellStyle;
	}

	/**
	 * 设置表体的单元格样式
	 * 
	 * @return
	 */
	public HSSFCellStyle getBodyStyle() {
		// 创建单元格样式
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		// 设置单元格居中对齐
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 设置单元格垂直居中对齐
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		// 创建单元格内容显示不下时自动换行
		cellStyle.setWrapText(true);
		// 设置单元格字体样式
		HSSFFont font = workbook.createFont();
		// 设置字体加粗
		font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		font.setFontName("宋体");
		font.setFontHeight((short) 200);
		cellStyle.setFont(font);
		// 设置单元格边框为细线条
		/*
		 * cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		 * cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		 * cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		 * cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		 */
		return cellStyle;
	}

	public String[] getHeaders() {
		return headers;
	}

	public void setHeaders(String[] headers) {
		this.headers = headers;
	}

	public String[] getHeaderAttributes() {
		return headerAttributes;
	}

	public void setHeaderAttributes(String[] headerAttributes) {
		this.headerAttributes = headerAttributes;
	}

	public List<Map<String, Object>> getDataList() {
		return dataList;
	}

	public void setDataList(List<Map<String, Object>> dataList) {
		this.dataList = dataList;
	}

	public String getFirstSheetName() {
		return firstSheetName;
	}

	public void setFirstSheetName(String firstSheetName) {
		this.firstSheetName = firstSheetName;
	}

	public static void main(String[] args) {
  	   /*BaseExportToExcel  export = new BaseExportToExcel(headers_,headerAttributes_) ;
        export.setDataList(dataList); 
        try {
            export.exportToExcel(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } */
    }
	
//	private static  final String[] headers_ = new String[]{"日期","模块","活跃用户 ","活跃用户占比"};
//    private static  final String[] headerAttributes_ = new String[]{"thedate","moduleName","activeUser","activeUserCr"};
 
    
}
